﻿<%
Function BackupDBPro_Snack
	Call BackupDBPro_Initialize
	Dim a
	a = DateDIff("d",BackupDBPro_Lastbak,Date)
	If a > BackupDBPro_Interval and BackupDBPro_On = True Then
		If ZC_MSSQL_ENABLE = True Then Call BackupDBPro_Mssql("backup",1) Else Call BackupDBPro_Access(1)
		SetBlogHint_Custom("√ 已为您自动备份数据库。")
	End If
End Function

Function BackupDBPro_SaveConfig
	Set BackupDBPro_Config=New TConfig
	BackupDBPro_Config.Load "BackupDBPro"
	BackupDBPro_Config.Write "BackupDBPro_On",Request.Form("on")
	BackupDBPro_Config.Write "BackupDBPro_Interval",Request.Form("interval")
	BackupDBPro_Config.Save
End Function

Function BackupDBPro_Mssql(act,num)

	Dim bak_path, srv, bak, rest, Files, bak_name

	If act = "backup" Then
		bak_name = year(now) & right("0" & month(now),2) & right("0" & day(now),2) & right("0" & hour(now),2) & right("0" & minute(now),2) & right("0" & second(now),2) & ".bak"
		bak_path = BlogPath&"zb_users\DATA\"&bak_name
		'写入本次自动备份时间及备份文件名
		Set BackupDBPro_Config=New TConfig
		BackupDBPro_Config.Load "BackupDBPro"
		If num =1 Then BackupDBPro_Config.Write "BackupDBPro_Lastbak",CStr(Date)
		BackupDBPro_Config.Write "BackupDBPro_Backup"&num,bak_name
		BackupDBPro_Config.Save
		'删除之前的备份
		If num=1 Then
			Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup1)
		ElseIf num=2 Then
			Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup2)
		ElseIf num=3 Then
			Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup3)
		End If
		Set srv = Server.CreateObject("SQLDMO.SQLServer")
		srv.LoginTimeout = 15
		srv.Connect ZC_MSSQL_SERVER, ZC_MSSQL_USERNAME, ZC_MSSQL_PASSWORD
		Set bak = Server.CreateObject("SQLDMO.Backup")
		bak.Database = ZC_MSSQL_DATABASE
		bak.Devices = Files
		bak.Action = 0
		bak.Initialize = 1
		'bak.Replace = True
		bak.Files = bak_path
		bak.SQLBackup srv
	ElseIf act="restore" Then
		'恢复时要在没有使用数据库时进行！
		Call CloseConnect()
		If num=1 Then
			bak_path = BlogPath&"zb_users\DATA\"&BackupDBPro_Backup1
		ElseIf num=2 Then
			bak_path = BlogPath&"zb_users\DATA\"&BackupDBPro_Backup2
		ElseIf num=3 Then
			bak_path = BlogPath&"zb_users\DATA\"&BackupDBPro_Backup3
		End If
		Set srv=Server.CreateObject("SQLDMO.SQLServer")
		srv.LoginTimeout = 15
		srv.Connect ZC_MSSQL_SERVER, ZC_MSSQL_USERNAME, ZC_MSSQL_PASSWORD
		Set rest=Server.CreateObject("SQLDMO.Restore")
		rest.Action = 0 'full db restore
		rest.Database = ZC_MSSQL_DATABASE
		rest.Devices = Files
		rest.Files = bak_path
		rest.ReplaceDatabase=True 'Force restore over existing database
		rest.SQLRestore srv
	End If

End Function

Function BackupDBPro_Access(num)
	Dim fso, DBPath, strDBPath, bak_name
	DBPath = BlogPath&ZC_DATABASE_PATH
	strDBPath = Left(DBPath,instrrev(DBPath,"\"))
	bak_name = year(now) & right("0" & month(now),2) & right("0" & day(now),2) & right("0" & hour(now),2) & right("0" & minute(now),2) & right("0" & second(now),2) & ".mdb"
	Set BackupDBPro_Config=New TConfig
	BackupDBPro_Config.Load "BackupDBPro"
	If num =1 Then BackupDBPro_Config.Write "BackupDBPro_Lastbak",CStr(Date)
	BackupDBPro_Config.Write "BackupDBPro_Backup"&num,bak_name
	BackupDBPro_Config.Save
	'删除之前的备份
	If num=1 Then
		Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup1)
	ElseIf num=2 Then
		Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup2)
	ElseIf num=3 Then
		Call DelToFile(BlogPath&"/zb_users/DATA/"&BackupDBPro_Backup3)
	End If
	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(DBPath) Then
		fso.CopyFile dbpath,strDBPath&bak_name
	End If
End Function
%>